package com.xsd.awen.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsd.awen.config.WebResult;
import com.xsd.awen.domain.entity.Task;
import com.xsd.awen.domain.po.TaskPagePo;
import com.xsd.awen.domain.vo.TaskVo;
import com.xsd.awen.mapper.TaskMapper;
import com.xsd.awen.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

@Service
@Scope(proxyMode= ScopedProxyMode.TARGET_CLASS)
public class TaskService {

    @Autowired
    TaskMapper taskMapper;

    public PageInfo<TaskPagePo> page(TaskVo taskVo){
        PageHelper.startPage(taskVo.getPage(),taskVo.getLimit());
        return new PageInfo<>(taskMapper.page(taskVo));
    }

    public String save(Task task){
        task.setId(UUIDUtils.generate());
        taskMapper.insert(task);
        return WebResult.OK();
    }

    public String update(Task task){
        taskMapper.updateById(task);
        return WebResult.OK();
    }

    public String delete(Task task){

        Integer count =  taskMapper.countUserTaskByTaskId(task);
        if( count != null && count > 0 ){
            return WebResult.ERROR("任务下有任务人员，不能删除");
        }else{

            Task dbTask = taskMapper.selectById(task.getId());
            if( dbTask.getStatus().intValue() == 100 ){
                taskMapper.deleteById(task);
            }
            return WebResult.OK();
        }
    }

    public Task getById(String id){
        Task task = taskMapper.selectById(id);
        return task;
    }

}
